<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-05-22 14:32:05
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-07-19 13:54:18
 * @FilePath: \巡检管理系统\巡检轨迹
 *
-->
<script setup lang="tsx">
// 巡检轨迹
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref, reactive } from 'vue'
import TrackPlayback from './components/TrackPlayback.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import PatrolInfoApi from '@/api/linePatrol'
import type { PatrolInfList } from '@/api/linePatrol/type'
import { ElMessage } from 'element-plus'
import { useTrackPlayStore } from '@/store/modules/trackplayback'
const TrackPlayStore = useTrackPlayStore()
const ids = ref<string[]>([])
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const i = await PatrolInfoApi.PatrolInfolist({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: i.rows,
      total: i.total
    }
  },
  fetchDelApi: async () => {
    const res = await PatrolInfoApi.DeletpatrolInfo(unref(ids)[0])
    console.log(res)
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { t } = useI18n()
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    search: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      type: 'selection'
    }
  },
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    search: {
      hidden: true
    },

    detail: {
      hidden: true
    }
  },
  {
    field: 'firmName',
    label: t('所属子公司'),
    search: {
      component: 'Input',
      componentProps: {
        placeholder: '所属子公司'
      }
    }
  },
  {
    field: 'user',
    label: t('巡检人'),
    search: {
      component: 'Input',
      componentProps: {
        placeholder: '巡检人'
      }
    }
  },

  {
    field: 'startDate',
    label: t('开始时间'),
    width: 150,
    search: {
      hidden: true
    }
  },
  {
    field: 'endDete',
    label: t('结束时间'),
    width: 150,
    search: {
      hidden: true
    }
  },
  {
    field: 'duration',
    label: t('巡线时长（小时）'),
    search: {
      hidden: true
    }
  },
  {
    field: 'mileage',
    label: t('巡线里程（km）'),
    search: {
      hidden: true
    }
  },
  {
    field: 'posnNo',
    label: t('定位数量'),
    search: {
      hidden: true
    }
  },
  {
    field: 'passRate',
    label: t('定位合格率'),
    search: {
      hidden: true
    }
  },
  {
    field: 'action',
    width: '200px',
    label: t('tableDemo.action'),
    search: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <span class="TableBtn" onClick={() => action(data.row, 'Detail')}>
                {t('查看')}
              </span>
              <span class="TableBtn" onClick={() => action(data.row, 'edit')}>
                {t('补自动打卡')}
              </span>
              <span class="TableBtn" style="color:#f56c6c" onClick={() => delData(data.row)}>
                {t('删除')}
              </span>
            </>
          )
        }
      }
    }
  }
])
// @ts-ignore
const { allSchemas } = useCrudSchemas(crudSchemas)
const dialogVisible = ref(false)
const actionType = ref('')
const delLoading = ref(false)
//删除轨迹
const delData = async (row: PatrolInfList) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.uuid] : elTableExpose?.getSelectionRows().map((v: any) => v.uuid) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
//表格操作
const action = (row: PatrolInfList, type: string) => {
  actionType.value = type
  if (type == 'Detail') {
    TrackPlayStore.setTrackPlayList(row.uuid)
    dialogVisible.value = true //打开弹窗
  } else {
    ElMessage.success('已补自动打卡')
  }
}
//轨迹回放Ref
const TrackPlaybackRef = ref<ComponentRef<typeof TrackPlayback>>()
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      row-key="uuid"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" style="width: 1200px" title="巡检轨迹" maxHeight="600px">
    <TrackPlayback v-if="actionType === 'Detail'" ref="TrackPlaybackRef" />
  </Dialog>
</template>
<style scoped>
/* .el-dialog {
  width: 80%;
  height: 90%;
} */
.el-dialog__body {
  height: 90% !important;
}
.el-scrollbar {
  height: 100% !important;
}
:deep(.TableBtn) {
  margin: 0 5px;
  cursor: pointer;
  color: #409eff;
  font-weight: 600;
}
</style>
